package com.example.health_life.controller;

import com.example.health_life.Response;
import com.example.health_life.dao.MyFavorite;
import com.example.health_life.service.MyFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class MyFavoriteController {

    @Autowired
    private MyFavoriteService myfavoriteService;

    @GetMapping("/myfavorites")
    public Response<List<MyFavorite>> getMyfavoriteList(@RequestParam(required = false,defaultValue = "1") Integer status) {
        return Response.newSuccess(myfavoriteService.getMyfavoriteList(status));
    }

    @GetMapping("/myfavorites/{id}")
    public Response<MyFavorite> getMyfavoriteById(@PathVariable Integer id) {
        return Response.newSuccess(myfavoriteService.getMyfavoriteById(id));
    }

    @GetMapping("/myfavorites/user/{userId}")
    public Response<List<MyFavorite>> getMyfavoriteByUserId(@PathVariable Integer userId,@RequestParam(required = false,defaultValue = "1") Integer status) {
        return Response.newSuccess(myfavoriteService.getMyfavoriteByUserId(userId,status));
    }

    @GetMapping("/myfavorites/type/{type}")
    public Response<List<MyFavorite>> getMyfavoriteByTypeId(@PathVariable Byte type,@RequestParam(required = false,defaultValue = "1") Integer status) {
        return Response.newSuccess(myfavoriteService.getMyfavoriteByTypeId(type,status));
    }

    @DeleteMapping("/myfavorites/{id}")
    public Response<Integer> deleteMyfavorite(@PathVariable Integer id) {
        MyFavorite fav = myfavoriteService.getMyfavoriteById(id);
        myfavoriteService.deleteMyfavorite(id);
        // 收藏类型：1-新闻，2-文章
        if (fav.getType() == 1) {
            myfavoriteService.decreaseFavoriteCountForNews(fav.getTypeId());
        } else if (fav.getType() == 2) {
            myfavoriteService.decreaseFavoriteCount(fav.getTypeId());
        }
        return Response.newSuccess(id);
    }

    @PostMapping("/myfavorites")
    public Response<Integer> addMyfavorite(@RequestBody MyFavorite myfavorite) {
        System.out.println("收到的收藏对象：" + myfavorite);
        Integer id = myfavoriteService.addMyfavorite(myfavorite);
        // 收藏类型：1-新闻，2-文章
        if (myfavorite.getType() == 1) {
            myfavoriteService.increaseFavoriteCountForNews(myfavorite.getTypeId());
        } else if (myfavorite.getType() == 2) {
            myfavoriteService.increaseFavoriteCount(myfavorite.getTypeId());
        }
        return Response.newSuccess(id);
    }

    @PostMapping("/articles/{articleId}/favorite/increase")
    public Response<Integer> increaseFavoriteCount(@PathVariable Integer articleId) {
        return Response.newSuccess(myfavoriteService.increaseFavoriteCount(articleId));
    }

    @PostMapping("/articles/{articleId}/favorite/decrease")
    public Response<Integer> decreaseFavoriteCount(@PathVariable Integer articleId) {
        return Response.newSuccess(myfavoriteService.decreaseFavoriteCount(articleId));
    }

    @PostMapping("/news/{newsId}/favorite/increase")
    public Response<Integer> increaseFavoriteCountForNews(@PathVariable Integer newsId) {
        return Response.newSuccess(myfavoriteService.increaseFavoriteCountForNews(newsId));
    }

    @PostMapping("/news/{newsId}/favorite/decrease")
    public Response<Integer> decreaseFavoriteCountForNews(@PathVariable Integer newsId) {
        return Response.newSuccess(myfavoriteService.decreaseFavoriteCountForNews(newsId));
    }
}
